package kiven.jee.utils.encrypter.md5;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

import kiven.jee.utils.encrypter.IPasswordEncrypter;

public class PasswordMd5Encrypter implements IPasswordEncrypter {

	@Override
	public String encryptPassword(String password) {
		if (password == null)
			password = "";

		// 加密
		byte[] encryptedPassword = Base64.encodeBase64(DigestUtils
				.md5(password));

		return new String(encryptedPassword);
	}

	@Override
	public boolean isPasswordValid(String password, String encryptedPassword) {
		boolean flag = false;
		if (StringUtils.isBlank(encryptedPassword)
				&& StringUtils.isBlank(password)) {
			flag = true;
		} else {
			flag = encryptedPassword.endsWith(encryptPassword(password));
		}

		return flag;
	}

}
